﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace MultipleChoice_ExamGenerator
{
    /// <summary>
    /// Η κλάση TextBoxWithPH είναι μια κλάση που επεκτείνει την κλάση TextBox 
    /// δίνοντας την δυνατότητα προσθήκης PlaceHolder (όπως την HTML)
    /// </summary>
    internal class TextBoxWithPh : TextBox
    {
        #region Attributes with Getters - Setters

        private string _placeHolderText;
        private Color _placeHolderColor;
        private Font _placeHolderFont;
        private SolidBrush _placeHolderBrush;
        private Panel _placeHolderContainer;

        public string PlaceHolderText
        {
            get { return _placeHolderText; }
            set { _placeHolderText = value; Invalidate(); }
        }

        public Color PlaceHolderColor
        {
            get { return _placeHolderColor; }
            set { _placeHolderColor = value; Invalidate(); }
        }

        #endregion

        // Constructor
        public TextBoxWithPh()
        {
            _placeHolderText = "Default Place Holder Text";
            _placeHolderColor = Color.DarkGray;
            _placeHolderFont = new Font(this.Font, FontStyle.Italic);
            _placeHolderBrush = new SolidBrush(_placeHolderColor);

            CreatePlaceHolder();

            // Adding events
            TextChanged += TextBoxWithPh_TextChanged;
            FontChanged += TextBoxWithPh_FontChanged;
        }

        #region Events

        private void TextBoxWithPh_FontChanged(object sender, EventArgs e)
        {
            _placeHolderFont = new Font(this.Font, FontStyle.Italic);
        }

        private void TextBoxWithPh_TextChanged(object sender, EventArgs e)
        {
            if (TextLength <= 0)
            {
                CreatePlaceHolder();
            }
            else if (TextLength > 0)
            {
                RemovePlaceHolder();
            }
        }

        private void _placeHolderContainer_Paint(object sender, PaintEventArgs e)
        {
            _placeHolderContainer.Location = new Point(0, 0);
            _placeHolderContainer.Anchor = AnchorStyles.Left;
            _placeHolderContainer.Height = this.Height;
            _placeHolderContainer.Width = this.Width;
            _placeHolderBrush = new SolidBrush(_placeHolderColor);

            var waterFormat = StringFormat.GenericDefault;
            waterFormat.Alignment = StringAlignment.Center;

            var graphic = e.Graphics;
            graphic.DrawString(_placeHolderText, _placeHolderFont, _placeHolderBrush, _placeHolderContainer.ClientRectangle, waterFormat);
        }

        private void _placeHolderContainer_Click(object sender, EventArgs e)
        {
            Focus();
        }

        #endregion

        /// <summary>
        /// Η μέθοδος CreatePlaceHolder δημιουργεί και προσθέτει το Panel στο TextBox που περιέχει το κείμενο του PlaceHolder.
        /// Επίσης προσθέτει δύο events στο Panel, το Click και το Paint.
        /// </summary>
        private void CreatePlaceHolder()
        {
            _placeHolderContainer = new Panel();
            this.Controls.Add(_placeHolderContainer);

            // Adding events
            _placeHolderContainer.Click += _placeHolderContainer_Click;
            _placeHolderContainer.Paint += _placeHolderContainer_Paint;
        }

        /// <summary>
        /// Η μέθοδος RemovePlaceHolder αφαιρεί το Panel απο το TextBox
        /// </summary>
        private void RemovePlaceHolder()
        {
            this.Controls.Remove(_placeHolderContainer);
        }

        protected override void OnInvalidated(InvalidateEventArgs e)
        {
            base.OnInvalidated(e);
            _placeHolderContainer.Invalidate();
        }

    }
}
